<?php
namespace app\index\controller;

use app\cms\model\News;
use think\Config;
use think\Controller;
use app\admin\model\Auth as AuthModel;
use app\student\model\Yy as YyModel;

class Index extends Controller
{
    public function index()
    {
        return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
    }

    public function getauthlistbymid(){
        $mid = $this->request->param('mid');
        if($mid){
            $plist = AuthModel::where('pid',0)->where('is_menu',1)->where('menu_id',$mid)->select();
        }else{
            $plist = AuthModel::where('pid',0)->where('is_menu',1)->select();
        }
        $this->assign('plist',$plist);
        return json_encode($plist);
    }

    public function uploadimg(){
        header("Access-Control-Allow-Origin: *");

        if($_FILES["file"]["error"] > 0){
            $res = ['statusCode'=>300,'message'=>'error:'.$_FILES["file"]["error"]];
        }else{
            // 取得文件后缀名
            $fileuploadurl = "upload/img/". date('Ymd') . '/';
            $file_fix = explode('.', $_FILES['file']['name'])[1] ? explode('.', $_FILES['file']['name'])[1] : 'png';
            // 文件重命名，这里自动生成一个不重复的名字，方便使用
            $name = md5(uniqid(md5(microtime(true)), true)) . '.' . $file_fix;
            // 检测要存放文件的目录是否存在，不存在则创建
            if (!is_dir($fileuploadurl)) {
                mkdir($fileuploadurl, 0755, true);
            }
            // 移动文件到指定目录下
            move_uploaded_file($_FILES["file"]["tmp_name"], $fileuploadurl . $name);
            list($width, $height, $type, $attr) = getimagesize($fileuploadurl . $name);
            $res = ['errno'=> '0','statueCode'=>200,'code'=>0,'msg'=>'文件上传成功','message'=>'上传成功','data'=>
                ['src'=>Config::get('view_replace_str')['__PUBLIC__'].'/'.$fileuploadurl . $name
                    ,'title'=>$name]
            ];
        }
        echo json_encode($res);
    }

    public function uploadvideo(){
        header("Access-Control-Allow-Origin: *");

        if($_FILES["file"]["error"] > 0){
            $res = ['statusCode'=>300,'message'=>'error:'.$_FILES["file"]["error"]];
        }else{
            // 取得文件后缀名
            $fileuploadurl = "upload/video/". date('Ymd') . '/';
            $file_fix = explode('.', $_FILES['file']['name'])[1] ? explode('.', $_FILES['file']['name'])[1] : 'png';
            // 文件重命名，这里自动生成一个不重复的名字，方便使用
            $name = md5(uniqid(md5(microtime(true)), true)) . '.' . $file_fix;
            // 检测要存放文件的目录是否存在，不存在则创建
            if (!is_dir($fileuploadurl)) {
                mkdir($fileuploadurl, 0755, true);
            }
            // 移动文件到指定目录下
            move_uploaded_file($_FILES["file"]["tmp_name"], $fileuploadurl . $name);
            $res = ['statueCode'=>200,'code'=>0,'msg'=>'文件上传成功','message'=>'上传成功','data'=>[
                'src'=>Config::get('view_replace_str')['__PUBLIC__'].'/'.$fileuploadurl . $name
                ,'title'=>$name]
            ];
        }
        echo json_encode($res);
    }

    public function uploadfile(){
        header("Access-Control-Allow-Origin: *");

        if($_FILES["file"]["error"] > 0){
            $res = ['statusCode'=>300,'message'=>'error:'.$_FILES["file"]["error"]];
        }else{
            // 取得文件后缀名
            $fileuploadurl = "upload/file/". date('Ymd') . '/';
            $file_fix = explode('.', $_FILES['file']['name'])[1] ? explode('.', $_FILES['file']['name'])[1] : 'png';
            // 文件重命名，这里自动生成一个不重复的名字，方便使用
            $name = md5(uniqid(md5(microtime(true)), true)) . '.' . $file_fix;
            // 检测要存放文件的目录是否存在，不存在则创建
            if (!is_dir($fileuploadurl)) {
                mkdir($fileuploadurl, 0755, true);
            }
            // 移动文件到指定目录下
            move_uploaded_file($_FILES["file"]["tmp_name"], $fileuploadurl . $name);
            list($width, $height, $type, $attr) = getimagesize($fileuploadurl . $name);
            $res = ['errno'=> '0','statueCode'=>200,'code'=>0,'msg'=>'文件上传成功','message'=>'上传成功','data'=>
                ['src'=>Config::get('view_replace_str')['__PUBLIC__'].'/'.$fileuploadurl . $name
                    ,'title'=>$name]
            ];
        }
        echo json_encode($res);
    }



    public function deletefile(){
        header("Access-Control-Allow-Origin: *");
        $i = 0;
        $imgpath = $this->request->param('imgpath');
        $d = explode('upload',$imgpath);
        $d = 'upload'.$d[1];
        if(file_exists($d)){
            unlink($d);
        }else{
            $i++;
        }
        $filepath = $this->request->param('filepath');
        $d = explode('upload',$filepath);
        $d = 'upload'.$d[1];
        unlink($d);
        if(file_exists($d)){
            unlink($d);
        }else{
            $i++;
        }
        if($i>0){
            $res = ['code'=>0,'msg'=>'操作完成'];
        }else{
            $res = ['code'=>1,'msg'=>'操作失败'];
        }
        echo json_encode($res);
    }

    public function yy(){
        header("Access-Control-Allow-Origin: *");
        $data = array(
            'name' => $this->request->param('name'),
            'phone' => $this->request->param('phone'),
            'mark' => 1 ,
            'opttime' => time()
        );
        if(count($data)>=4){
            YyModel::create($data);
            $res = ['statusCode'=>200,'message'=>'预约成功'];
        }else{
            $res = ['statusCode'=>300,'message'=>'请正确填入相关信息'];
        }
        echo json_encode($res);
    }

    public function getNewsList()
    {
        $datas = News::where('checked',1)->order('newstime','desc')->select();
        echo json_encode($datas);
    }

    public function getNewinfoByid(){
        $id = $this->request->param('newsid');
        $news = News::get($id);
        $update = News::where('id',$id)->update(['onclick'=>($news->onclick+1)]);
        $data = News::where('checked',1)->where('id',$id)->find();
        echo json_encode($data);
    }

}
